package com.md.common.utils.sms.submail;

import com.md.common.utils.json.JSONUtil;
import com.md.common.utils.sms.SmsReqDto;
import com.md.common.utils.sms.submail.resp.SubmailSMSResp;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class SubmailSMSUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(SubmailSMSUtils.class);

    public static String appId = "34244";
    public static String appKey = "fedb74dea798833eab2cea2b42a62782";

    /**
     * API 请求接口配置
     */
    private static final String URL = "https://api.mysubmail.com/message/send";

    public static SubmailSMSResp send(String mobile, String content) {
        SmsReqDto smsReqDto = new SmsReqDto();
        smsReqDto.setMobile(mobile);
        smsReqDto.setContent(content);
        return send(smsReqDto);
    }

    public static SubmailSMSResp send(SmsReqDto sms) {
        String bizDesc = "调用submail短信接口";
        LOGGER.info("{} start, {}", bizDesc, sms.getContent());

        try{

            BasicNameValuePair appid = new BasicNameValuePair("appid", appId);
            BasicNameValuePair content = new BasicNameValuePair("content", sms.getContent());
            BasicNameValuePair to = new BasicNameValuePair("to", sms.getMobile());
            BasicNameValuePair sign = new BasicNameValuePair("signature", appKey);
            List<NameValuePair> params = Arrays.asList(appid, content, to, sign);

            HttpPost httpPost = new HttpPost(URL);
            httpPost.addHeader("charset", "UTF-8");

            httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

            CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
            HttpResponse response = closeableHttpClient.execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if(httpEntity != null){
                String jsonStr = EntityUtils.toString(httpEntity, "UTF-8");
                LOGGER.info("{}", jsonStr);
                return JSONUtil.toBean(jsonStr, SubmailSMSResp.class);
            }
        } catch(IOException e){
            e.printStackTrace();
        }

        LOGGER.info("{} end", bizDesc);
        return null;
    }

    public static void main(String[] args) {
//        SmsReqDto smsReqDto = new SmsReqDto();
//        smsReqDto.setContent("【医生远程协作子系统】您的检查结果已出，请您到放射科领取检查结果。");
//        smsReqDto.setContent("【Test】test");
//        smsReqDto.setMobile("18665009229");
//        send(smsReqDto);
    }

}
